﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKRevitKernel.ElementSearchUtility
{
	public static class ElementSearchMethod
	{
		public static List<Element> GetInsectionElementByDoc(Autodesk.Revit.DB.Document doc, Solid solid)
		{
			new List<int>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ElementIntersectsSolidFilter elementIntersectsSolidFilter = new ElementIntersectsSolidFilter(solid);
			IEnumerable<Element> enumerable = filteredElementCollector.WherePasses(elementIntersectsSolidFilter).ToElements();
			List<Element> list = new List<Element>();
			foreach (Element item in enumerable)
			{
				list.Add(item);
			}
			return list;
		}

		public static ViewType GetCurrentViewType(Autodesk.Revit.DB.Document doc)
		{
			return doc.ActiveView.ViewType;
		}

		public static Level GetCurrentViewLevel(Autodesk.Revit.DB.Document doc)
		{
			return doc.ActiveView.GenLevel;
		}

		public static List<Level> GetAllLevel(Autodesk.Revit.DB.Document doc)
		{
			return (from l in new FilteredElementCollector(doc).OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels)
			orderby ((Level)l).Elevation
			select l).Cast<Level>().ToList<Level>();
		}

		public static List<Level> GetAllLevel(Autodesk.Revit.DB.Document doc, Level minLevel, Level maxLevel)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			new List<Level>();
			List<Level> list = new List<Level>();
			if (minLevel == null && maxLevel == null)
			{
				list = (from l in filteredElementCollector.OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels)
				orderby ((Level)l).Elevation
				select l).Cast<Level>().ToList<Level>();
			}
			else
			{
				if (minLevel == null && maxLevel != null)
				{
					using (List<Level>.Enumerator enumerator = (from l in filteredElementCollector.OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels)
					orderby ((Level)l).Elevation
					select l).Cast<Level>().ToList<Level>().GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Level level = enumerator.Current;
							if (level.Elevation <= maxLevel.Elevation)
							{
								list.Add(level);
							}
						}
						return list;
					}
				}
				if (minLevel != null && maxLevel == null)
				{
					using (List<Level>.Enumerator enumerator = (from l in filteredElementCollector.OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels)
					orderby ((Level)l).Elevation
					select l).Cast<Level>().ToList<Level>().GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Level level2 = enumerator.Current;
							if (level2.Elevation >= minLevel.Elevation)
							{
								list.Add(level2);
							}
						}
						return list;
					}
				}
				if (minLevel != null && maxLevel != null)
				{
					foreach (Level level3 in (from l in filteredElementCollector.OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels)
					orderby ((Level)l).Elevation
					select l).Cast<Level>().ToList<Level>())
					{
						if (level3.Elevation >= minLevel.Elevation && level3.Elevation <= maxLevel.Elevation)
						{
							list.Add(level3);
						}
					}
				}
			}
			return list;
		}

		public static Level GetLevelByName(Autodesk.Revit.DB.Document doc, string strName)
		{
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Levels);
			IEnumerable<Element> enumerable = new FilteredElementCollector(doc).OfClass(typeof(Level)).WherePasses(elementCategoryFilter).ToElements();
			Level result = null;
			foreach (Element element in enumerable)
			{
				Level level = element as Level;
				if (level.Name.ToString().Equals(strName))
				{
					result = level;
				}
			}
			return result;
		}

		public static GridType GetGridTypeByName(Autodesk.Revit.DB.Document doc, string strName)
		{
			GridType result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(GridType));
			foreach (Element element in filteredElementCollector)
			{
				GridType gridType = (GridType)element;
				if (gridType.Name.ToString().Equals(strName))
				{
					result = gridType;
				}
			}
			return result;
		}

		public static WallType GetWallTypeByName(Autodesk.Revit.DB.Document doc, string strName)
		{
			WallType result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(WallType));
			foreach (Element element in filteredElementCollector)
			{
				WallType wallType = (WallType)element;
				if (wallType.Name.ToString().Equals(strName))
				{
					result = wallType;
				}
			}
			return result;
		}

		public static DimensionType GetDimTypeByName(Autodesk.Revit.DB.Document doc, string strName, DimensionStyleType styleType)
		{
			DimensionType result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(DimensionType));
			foreach (Element element in filteredElementCollector)
			{
				DimensionType dimensionType = (DimensionType)element;
				if (dimensionType.Name.ToString().CompareTo(strName) == 0 && dimensionType.StyleType == styleType)
				{
					result = dimensionType;
					break;
				}
			}
			return result;
		}

		public static ICollection<ElementId> GetAllFamilyInstance(Autodesk.Revit.DB.Document doc, BuiltInCategory builtInCategory)
		{
			return new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(builtInCategory).ToElementIds();
		}

		public static List<TextNoteType> GetSystemFontTypeArray(Autodesk.Revit.DB.Document doc)
		{
			List<TextNoteType> list = new List<TextNoteType>();
			try
			{
				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).ToElements())
				{
					TextNoteType textNoteType = element as TextNoteType;
					if (textNoteType != null && !textNoteType.Name.Contains("HY_Leader_"))
					{
						list.Add(textNoteType);
					}
				}
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
			}
			return list;
		}

		public static List<string> GetSystemFontTypeStringArray(Autodesk.Revit.DB.Document doc)
		{
			List<string> list = new List<string>();
			foreach (TextNoteType textNoteType in ElementSearchMethod.GetSystemFontTypeArray(doc))
			{
				if (!(textNoteType.Name == "LZ_TXT_0.0001"))
				{
					list.Add(textNoteType.Name);
				}
			}
			return list;
		}
	}
}
